package doc2db.interfaces.action.open;

import doc2db.Keey;
import doc2db.Launcher;
import doc2db.interfaces.action.Doc2DbAction;
import doc2db.interfaces.action.AbstractGUIAction;
import doc2db.interfaces.mainWindow.MyFrame;
import doc2db.interfaces.mainWindow.MyPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.opu.gui.docviewer.DocUTIL;

/**
 *
 * @author yura
 */
public class OpenFile extends AbstractGUIAction {
    private MyPanel pane;
    private MyFrame frame;
    private JScrollPane root;
    private JPanel south;

    @Override
    public boolean start() {
        JScrollPane sr = pane.textWeekendInformation.getRoot();
        if (! sr.isVisible()) {
            sr.setVisible(true);
        }
        return true;
    }

    @Override
    public boolean end(){
        if (root != null){
            root.setViewportView(pane.textWeekendInformation);
            pane.file.setText("");
        }
        if (south != null) {
            south.setVisible(false);
            south = null;
            pane.textWeekendInformation.getRootPnl().revalidate();
            pane.textWeekendInformation.getRootPnl().repaint();
        }
        return true;
    }

    @Override
    public void action(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(frame.getTitle() + ":Открыть файл");

        File file = new File(Keey.lastSelectedFile);

        if (! file.exists()){
            Keey.lastSelectedFile = ".";
            file = new File(Keey.lastSelectedFile);
        }
        chooser.setCurrentDirectory(file);
        
        String[] arr = new String[]{"doc", "DOC"};
        String defenition = "Microsoft Word document";
        chooser.setFileFilter(new FileNameExtensionFilter( defenition, arr));

        int r = chooser.showOpenDialog(frame);

        if (r == JFileChooser.APPROVE_OPTION) {
            final String fname = chooser.getSelectedFile().getAbsolutePath();
            Keey.lastSelectedFile = fname;
            openFile(fname);
        }
    }

    public OpenFile(MyFrame frame, MyPanel pane) {
        this.frame = frame;
        this.pane = pane;
        this.root = null;

    }

    public void openFile(final String fname) {
        pane.p.setVisible(true);
        if (("" + pane.p.isVisible()).equals("false")) {
            System.err.println("error, may be ...");
        }
        try {
            JEditorPane docPane = DocUTIL.getDocPane(fname);
            docPane.setBackground(Color.white);
            root = pane.textWeekendInformation.getRoot();
            root.setViewportView(docPane);
            pane.file.setText(fname);

            JButton submfile = new JButton("submit", Launcher.getIcon("pane/submit.png"));
            submfile.addActionListener(new Doc2DbAction(frame, pane.file,
                    pane.fname, pane.textDBSelects, pane.textWeekendInformation));
            submfile.setSize(24, 24);
            south = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
            south.add(submfile);
            JPanel rootPnl = pane.textWeekendInformation.getRootPnl();
            rootPnl.add(south, BorderLayout.NORTH);
            rootPnl.revalidate();
            rootPnl.repaint();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(frame, "can open file \"" + fname + "\": " + ex);
        }
        pane.p.setVisible(false);
    }

}
